package com.itheima.trade.service.impl;

import com.itheima.model.common.dtos.AccountDto;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.feign.fallback.UserClient;
import com.itheima.model.common.pojos.Account;
import com.itheima.trade.mapper.AccountMapper;
import com.itheima.trade.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;


    @Autowired
    private UserClient userClient;

    // 转账
    @Transactional
    @Override
    public ResponseResult transfer(@RequestBody AccountDto fromAccount, @RequestBody AccountDto toAccount) {
        //获取转账金额
        BigDecimal transferAmount = fromAccount.getTransferAmount();

        //根据用户id和账号查询用户信息
        Account account = accountMapper.getAccount(fromAccount.getAid(), fromAccount.getCardno());

        //获取账户余额
        BigDecimal remaining = account.getRemaining();
        //判断余额是否足够
        BigDecimal i = remaining.subtract(transferAmount).setScale(2);
        if (i.intValueExact() < 0) {
            return ResponseResult.errorResult(500, "余额不足");
        }
        //根据转出账户查询账户信息
        Account account1 = accountMapper.getAccount1(toAccount.getCardno());
        if (account1 == null) {
            return ResponseResult.errorResult(500, "账户不存在");
        }
        accountMapper.updateAccount(account.getCardno(), i);

        BigDecimal j = account1.getRemaining().add(transferAmount);

        accountMapper.updateAccount(account1.getCardno(), j);

        return ResponseResult.okResult("转账成功");
    }

    // 存钱
    @Transactional
    @Override
    public ResponseResult saving(AccountDto accountDto) {
        //查询账号信息
        Account account = accountMapper.getAccount(accountDto.getAid(), accountDto.getCardno());
        //判断账号是否存在
        if (account == null) {
            return ResponseResult.errorResult(500, "账号不存在");
        }
        //获取余额+存钱金额
        BigDecimal remaining = account.getRemaining().add(accountDto.getTransferAmount());
        accountMapper.updateAccount(account.getCardno(), remaining);

        return ResponseResult.okResult("存钱成功");
    }

    // 取钱
    @Transactional
    @Override
    public ResponseResult withdrawMoney(AccountDto accountDto) {
        //查询账号信息
        Account account = accountMapper.getAccount(accountDto.getAid(), accountDto.getCardno());
        //判断账号是否存在
        if (account == null) {
            return ResponseResult.errorResult(500, "账号不存在");
        }
        //获取余额-取钱金额
        BigDecimal i = account.getRemaining().subtract(accountDto.getTransferAmount()).setScale(2);

        if (i.intValueExact() < 0) {
            return ResponseResult.errorResult(500, "余额不足");
        }

        accountMapper.updateAccount(account.getCardno(), i);
        return ResponseResult.okResult("取钱成功");
    }
}
